{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "42f559a3",
   "metadata": {},
   "source": [
    "# 索引与切片\n",
    "\n",
    "## 索引\n",
    "\n",
    "可以用中括号对字符串进行索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e48e3132",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'h'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = \"hello world\"\n",
    "s[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc634fc6",
   "metadata": {},
   "source": [
    "索引从0开始，索引第五个字符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d2674f04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'o'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8853272c",
   "metadata": {},
   "source": [
    "可以用负值从尾部索引，倒数第二个字符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6a8a93a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'l'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[-2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c71e1bc",
   "metadata": {},
   "source": [
    "## 切片\n",
    "\n",
    "可以用中括号提取子序列：\n",
    "\n",
    "```\n",
    "var[lower:upper:step]\n",
    "```\n",
    "\n",
    "其范围包括 `lower` ，但不包括 `upper` ，即 `[lower, upper)`， `step` 表示取值间隔大小，如果没有默认为1。\n",
    "\n",
    "取出索引位置1和3之间的字符串："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4bdc542c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'el'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[1:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da816968",
   "metadata": {},
   "source": [
    "切片中包含的元素的个数为 3-1=2 。\n",
    "\n",
    "也可以使用负索引来指定切片的范围："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e87d09d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ello wor'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[1:-2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4166ca7d",
   "metadata": {},
   "source": [
    "`lower`和`upper`可以省略，省略`lower`意味着从开头开始切片，省略`upper`意味着一直切片到结尾："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1052a0d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hel'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a4f8b2d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'rld'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[-3:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f437413",
   "metadata": {},
   "source": [
    "每隔两个取一个值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e2ebe1d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hlowrd'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[::2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ccac198",
   "metadata": {},
   "source": [
    "间隔为负数，则逆序输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9035fd19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'dlrow olleh'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10d1a476",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
